<?php
namespace app\back\controller;


use app\common\controller\AdminBase;
use think\Request;

///登录控制器
class Login extends AdminBase
{

    ///登录页面
    public function index(){
        ///检测登录状态
        $this->checkLoginStatus();

        ///调用页面
        return view();
    }


    ///登录操作
    public function login(Request $request){
        ///检测登录状态
        if(session('login_mark') && $request->isAjax()){
            $this->success('无需重复登录！',url('/'.ADMIN_ROUT_PREFIX.'/index'));
        }


        ///登录信息&管路员配置
        $client_password = $request->param('password','');
        $password = config('password.password');
        if(empty($client_password) || empty($password)){
            $this->error('参数不能为空！',url('/'.ADMIN_ROUT_PREFIX.'/login'));
        }
        if(!(password_verify($client_password, $password))){
            $this->error('账号或密码错误！',url('/'.ADMIN_ROUT_PREFIX.'/login'));
        }
        ///
        try{
            session('login_mark', null);
            ///登录时间
            $login_time = date('Y-m-d H:i:s');
            ///失效时间为6小时
            $expire_time = time()+6*3600;
            ///设置登录标识
            session('login_mark', array('key'=>md5($password.$login_time),'login_time'=>$login_time,'expire_time'=>$expire_time));
        }catch(\Exception $e){
            trace($e,'error');
            trace($e->getMessage(),'error');
            $this->error('出现异常，请刷新页面！',url('/'.ADMIN_ROUT_PREFIX.'/login'));
        }
        $this->success('登录成功！',url('/'.ADMIN_ROUT_PREFIX.'/index'));
    }


    ///检测登录状态
    final private function checkLoginStatus(){
        if(session('login_mark')){
            ///跳转到前台
            $this->success('无需重复登录','/'.ADMIN_ROUT_PREFIX.'/index','',1);
        }
    }

    ///退出
    public function quit(){
        ///删除session
        session('login_mark', null);
        ///跳转到前台
        $this->success('退出成功','/'.ADMIN_ROUT_PREFIX.'/login','',1);
    }


}